import Toast from "@/utils/Toast";
import BaseService from "@/api/BaseService";
import CacheUtils from "@/utils/CacheUtils";

/** 登录 */
class LoginService {

    /** 微信令牌登录 判断是否是新用户 */
    async WxCodeLogin() {
        try {
            const data = await new Promise((resolve) => {
                uni.login({
                    provider: "weixin",
                    success: resolve,
                });
            });
            const code = data.code;
            const res = await BaseService.getAttempLogin(code);
            if (!res.newUser) {
                CacheUtils.setCache("token", res.token);
            }
            return res
        } catch (err) {
            Toast.info(err)
        }
    }


    /** 判断是否登录 */
    hasToken(){
        if(!CacheUtils.getCache("token")){
            Toast.info("请先登录");
            return Promise.reject(new Error("未登录"));
        }
        return Promise.resolve(true);
    }

}

export default new LoginService();